﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;


namespace EasyToHelp.Common
{
    public static class EncryptionMD5
    {
        /// <summary>
        /// 获取md5加密字符串
        /// </summary>
        /// <param name="inputValue"></param>
        /// <returns></returns>
        public static string GetMd5(string inputValue)
        {
            //32位大写
            using (var md5 = MD5.Create())
            {
                var result = md5.ComputeHash(Encoding.UTF8.GetBytes(inputValue));
                var strResult = BitConverter.ToString(result);
                return strResult.Replace("-", "");                
            }
        }
        /// <summary>
        /// 创建字符串加密
        /// </summary>
        /// <param name="inputValue"></param>
        /// <returns></returns>
        public static string CreateMd5(string inputValue)
        {
            //32位大写
            using (var md5 = MD5.Create())
            {
                var data = md5.ComputeHash(Encoding.UTF8.GetBytes(inputValue));
                StringBuilder builder = new StringBuilder();
                // 循环遍历哈希数据的每一个字节并格式化为十六进制字符串 
                for (int i = 0; i < data.Length; i++)
                {
                    builder.Append(data[i].ToString());
                }
                return builder.ToString();
            }
        }
        public static string GetMD5(string inputValue)
        {
            //16位大写
            using (var md5 = MD5.Create())
            {
                var data = md5.ComputeHash(Encoding.UTF8.GetBytes(inputValue));
                StringBuilder builder = new StringBuilder();
                // 循环遍历哈希数据的每一个字节并格式化为十六进制字符串 
                for (int i = 0; i < data.Length; i++)
                {
                    builder.Append(data[i].ToString());
                }
                return  builder.ToString().Substring(8, 16);
            }

        }
        /// <summary>
        /// SHA1加密
        /// </summary>
        /// <param name="SourceString">原字符串</param>
        /// <returns></returns>
        public static string SHA1Encrypt(string SourceString)
        {
            byte[] StrRes = Encoding.Default.GetBytes(SourceString);
            HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
            StrRes = iSHA.ComputeHash(StrRes);
            StringBuilder EnText = new StringBuilder();
            foreach (byte iByte in StrRes)
            {
                EnText.AppendFormat("{0:x2}", iByte);
            }
            return EnText.ToString();
        }

        /// <summary>
        /// 使用RNGCryptoServiceProvider生成种子
        /// </summary>
        /// <returns></returns>
        public static int GetRandomSeed()
        {
            byte[] bytes = new byte[8];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);

        }
    }
}
